package com.pzh.guli.service.vod.service.imp;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.pzh.guli.service.vod.service.VodVideoService;
import com.pzh.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

@Service
@Slf4j
public class VodVideoServiceImp implements VodVideoService {
    @Autowired
    private VodProperties vodProperties;
    @Override
    //video上传服务
    public String uploadVideo(InputStream inputStream, String originalFileName) {
        String title = originalFileName.substring(0, originalFileName.lastIndexOf("."));
        UploadStreamRequest request=new UploadStreamRequest(vodProperties.getKeyid(),vodProperties.getKeysecret(),title,originalFileName,inputStream);
        UploadVideoImpl uploadVideo=new UploadVideoImpl();
        UploadStreamResponse response=uploadVideo.uploadStream(request);
        String videoId=response.getVideoId();
        if (StringUtils.isEmpty(videoId)){
            log.info("阿里云上传失败"+response.getCode()+"-"+ response.getMessage());
        }
        return videoId;
    }
    //video删除服务
    @Override
    public void removeVideo(String id) throws ClientException {
        DefaultAcsClient client=invokeClient(vodProperties.getKeyid(),vodProperties.getKeysecret());
        DeleteVideoRequest request=new DeleteVideoRequest();
        request.setVideoIds(id);
        DeleteVideoResponse response=client.getAcsResponse(request);
    }
    //批量删除video
    @Override
    public void removeVideoByList(List<String> stringList) throws ClientException {
        DefaultAcsClient client=invokeClient(vodProperties.getKeyid(),vodProperties.getKeysecret());
        DeleteVideoRequest request=new DeleteVideoRequest();
        StringBuffer buffer=new StringBuffer();
        int size=stringList.size();
        for (int i = 0; i < size; i++) {
             buffer.append(stringList.get(i));
             //拼接
             if (i == size-1||i%20==19){ //一次性最大批处理20条
                request.setVideoIds(buffer.toString());
                client.getAcsResponse(request);
             }
             else if (i<19){
                 buffer.append(",");
             }
        }
    }
    //获取点播信息
    @Override
    public String getPlayAuth(String videoId) throws ClientException {
        DefaultAcsClient client=invokeClient(vodProperties.getKeyid(),vodProperties.getKeysecret());
        GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
        //request.setAuthInfoTimeout(200L);
        request.setVideoId(videoId);
        GetVideoPlayAuthResponse response=client.getAcsResponse(request);
        return response.getPlayAuth();
    }

    //初始化服务端
    public static DefaultAcsClient invokeClient(String accessKeyId,String accessKeySecret){
       String region="cn-shanghai";//接入地区
        DefaultProfile profile=DefaultProfile.getProfile(region,accessKeyId,accessKeySecret);
        DefaultAcsClient client=new DefaultAcsClient(profile);
        return  client;
    }
}
